package com.hwadee.teachingprocessmanagementsystem.service.login.impl;

import com.hwadee.teachingprocessmanagementsystem.annotation.Role;
import com.hwadee.teachingprocessmanagementsystem.dto.LoginUserDto;
import com.hwadee.teachingprocessmanagementsystem.entity.Menu;
import com.hwadee.teachingprocessmanagementsystem.entity.Student;
import com.hwadee.teachingprocessmanagementsystem.entity.Teacher;
import com.hwadee.teachingprocessmanagementsystem.entity.User;
import com.hwadee.teachingprocessmanagementsystem.service.base.MenuService;
import com.hwadee.teachingprocessmanagementsystem.service.base.StudentService;
import com.hwadee.teachingprocessmanagementsystem.service.base.TeacherService;
import com.hwadee.teachingprocessmanagementsystem.service.base.UserService;
import com.hwadee.teachingprocessmanagementsystem.service.login.LoginService;
import com.hwadee.teachingprocessmanagementsystem.util.JWTUtils;
import com.hwadee.teachingprocessmanagementsystem.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.hwadee.teachingprocessmanagementsystem.entity.User.getRole;

/**
 * @Description: 登录接口
 * @Author: Jukomu
 * @Package: com.hwadee.teachingprocessmanagementsystem.service.login.impl
 * @Project: TeachingProcessManagementSystem
 * @name: LoginServiceImpl
 * @Date: 2024/7/11-20:37
 * @Filename: LoginServiceImpl
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;

    /**
     * @Description: Login User Verify Password 用户登录验证
     * @Author: Jukomu
     * @param user
     * @return
     */
    @Override
    public User verifyUser(User user) {
        User user1 = this.userService.queryByUid(user.getUid());
        if (user1 == null) {
            return null;
        }
        try {
            if (user1.getPassword().equals(MD5Utils.getMD5Str(user.getPassword()))) {
                // 密码正确
                return user1;
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }

    /**
     * @Description: 获取登录Token
     * @param user
     * @return
     */
    @Override
    public String getToken(User user) {
        Role role = getRole(user);
        Map<String, String> map = new HashMap<>();
        map.put("uid", user.getReferenceId());
        map.put("role",role.toString());
        // 获取JWT
        return JWTUtils.getToken(map);
    }

    /**
     * 获取登录用户数据传输对象
     * @param user
     * @return
     */
    @Override
    public LoginUserDto getLoginUserDto(User user) {
        List<Menu> menus = menuService.queryByRole(user);
        LoginUserDto loginUserDto = new LoginUserDto();
        if (user.getRole() == 1) {
            Student student = this.studentService.queryByStudentNumber(user.getReferenceId());
            loginUserDto.setUsername(student.getName());
        } else {
            Teacher teacher = this.teacherService.queryById(Integer.valueOf(user.getReferenceId()));
            loginUserDto.setUsername(teacher.getName());
        }
        loginUserDto.setUserInfo(user);
        loginUserDto.setToken(getToken(user));
        loginUserDto.setMenuList(menus);
        return loginUserDto;
    }
}
